package com.origin.ebuy.bargd.dao.order.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.origin.ebuy.bargd.dao.order.IOrderDao;
import com.origin.ebuy.dao.comm.BaseDao;
import com.origin.ebuy.dao.comm.ResultHandler;

public class OrderDaoImpl extends BaseDao implements IOrderDao {

	/**
	 * 插入订单
	 */
	@Override
	public long insert(String[] cols, Object[] parames) throws SQLException {
		return insert("orders", cols, parames);
	}

	@Override
	public JSONArray queryAllOrder(String table, String where, String[] cols)
			throws SQLException {
		ResultHandler handler = new ResultHandler() {
			@Override
			public JSONArray handler(ResultSet res) throws SQLException {
				JSONArray array = new JSONArray();
				while(res.next()){
					JSONObject jsonObject = new JSONObject();
					jsonObject.put("oid", res.getString(1));
					jsonObject.put("gid", res.getString(2));
					jsonObject.put("price", res.getString(3));
					jsonObject.put("date", res.getString(4));
					jsonObject.put("state", res.getString(5));
					jsonObject.put("gnumber", res.getString(6));
					
					jsonObject.put("uname", res.getString(8));
					jsonObject.put("gname", res.getString(9));
					jsonObject.put("gimage", res.getString(10));
					
					jsonObject.put("address", res.getString(11)+res.getString(12)+res.getString(7));
					
					/*jsonObject.put("gimage", res.getString(11));
					jsonObject.put("gimage", res.getString(12));*/
					
					
					array.add(jsonObject);
				}
				return array;
			}
		};
		Object[] params = null;
		return (JSONArray)query(table, cols, where, params, handler);
	}

	@Override
	public int updateOrderState(String id, String state) throws SQLException {
		String[] cols = {"state"};
		Object[] parames = {state,id};
		return update("orders", cols, "id=?", parames);
	}

}
